home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests / malloc / RCS / test.c,v < prev   
Encoding:
Text File  |  1992-07-17  |  4.1 KB  |  210 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  srv030:1.1 srv027:1.1 srv026:1.1 srv024:1.1 srv021:1.1 srv018:1.1 srv014:1.1 srv010:1.1 srv008:1.1 srv007:1.1 srv006:1.1 srv004:1.1;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     91.12.12.22.39.07;  author kupfer;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @Some tests for malloc.
  17. @
  18.  
  19.  
  20.  
  21. 1.1
  22. log
  23. @Initial revision
  24. @
  25. text
  26. @/* 
  27.  * test.c --
  28.  *
  29.  *    Some tests for malloc().
  30.  *
  31.  * Copyright 1991 Regents of the University of California
  32.  * Permission to use, copy, modify, and distribute this
  33.  * software and its documentation for any purpose and without
  34.  * fee is hereby granted, provided that this copyright
  35.  * notice appears in all copies.  The University of California
  36.  * makes no representations about the suitability of this
  37.  * software for any purpose.  It is provided "as is" without
  38.  * express or implied warranty.
  39.  */
  40.  
  41. #ifndef lint
  42. static char rcsid[] = "$Header: /sprite/lib/forms/RCS/proto.c,v 1.5 91/02/09 13:24:44 ouster Exp $ SPRITE (Berkeley)";
  43. #endif /* not lint */
  44.  
  45. #include <bstring.h>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <sys/wait.h>
  50. #include <vm.h>
  51.  
  52. #define PARENT_DOES_MALLOC    1
  53.  
  54. /* Forward references: */
  55.  
  56. void CheckBuffer(), FillBuffer();
  57. void DoChild(), DoParent();
  58.  
  59. main()
  60. {
  61.     char *buf;
  62.     int pageSize;
  63.     int fds[2];            /* descriptors for pipe */
  64.     int childPid;
  65.  
  66. #if PARENT_DOES_MALLOC
  67.     /* Try allocating something smaller than a page. */
  68.     buf = malloc(100);
  69.     strcpy(buf, "small buffer");
  70.     bzero(buf, 100);
  71.     free(buf);
  72.  
  73.     /* Try allocating an entire page. */
  74.     Vm_PageSize(&pageSize);
  75.     buf = malloc(pageSize);
  76.     bzero(buf, pageSize);
  77.  
  78.     /* 
  79.      * Write into the one-page buffer.  Fork.  Do some tests.  
  80.      */
  81.  
  82.     FillBuffer(buf, pageSize, 'a');
  83. #endif /* PARENT_DOES_MALLOC */
  84.     pipe(fds);
  85.  
  86.     childPid = fork();
  87.     switch (childPid) {
  88.     case -1:
  89.     perror("fork");
  90.     exit(1);
  91.     break;
  92.     case 0:
  93.     DoChild(buf, pageSize, fds[0]);
  94.     break;
  95.     default:
  96.     DoParent(buf, pageSize, fds[1]);
  97.     break;
  98.     }
  99.  
  100.     exit(0);
  101. }
  102.  
  103. /* 
  104.  * Verify that the child inherited the buffer correctly and can continue to
  105.  * do its own mallocs.  Verify that the parent can overwrite the buffer w/o
  106.  * the child seeing it.
  107.  */
  108. void
  109. DoChild(buf, bufSize, fd)
  110.     char *buf;
  111.     int bufSize;
  112.     int fd;            /* descriptor for pipe to parent */
  113. {
  114.     char *dummy;
  115.     char waitChar[10];
  116.  
  117. #if PARENT_DOES_MALLOC
  118.     CheckBuffer(buf, bufSize, 'a', "child");
  119. #endif
  120.  
  121. #if 0
  122.     Test_PutMessage("child: CR to continue\n");
  123.     Test_GetString(waitChar, 10);
  124. #endif
  125.     dummy = malloc(bufSize / 2);
  126.     if (dummy == NULL) {
  127.     fprintf(stderr, "child: first malloc failed.\n");
  128.     exit(1);
  129.     }
  130. #if PARENT_DOES_MALLOC
  131.     bcopy(buf, dummy, bufSize/2);
  132. #endif
  133.     free(dummy);
  134.     dummy = malloc(bufSize);
  135.     if (dummy == NULL) {
  136.     fprintf(stderr, "child: second malloc failed.\n");
  137.     exit(1);
  138.     }
  139. #if PARENT_DOES_MALLOC
  140.     bcopy(buf, dummy, bufSize);
  141. #endif
  142.     free(dummy);
  143.  
  144.     if (read(fd, dummy, 1) < 0) {
  145.     perror("child: read");
  146.     exit(1);
  147.     }
  148. #if PARENT_DOES_MALLOC
  149.     CheckBuffer(buf, bufSize, 'a', "child");
  150. #endif
  151.     free(dummy);
  152. }
  153.  
  154. void
  155. DoParent(buf, bufSize, fd)
  156.     char *buf;
  157.     int bufSize;
  158.     int fd;            /* descriptor for pipe to child */
  159. {
  160.     char dummy;
  161.     union wait status;
  162.  
  163. #if PARENT_DOES_MALLOC
  164.     CheckBuffer(buf, bufSize, 'a', "parent");
  165.     FillBuffer(buf, bufSize, 'b');
  166. #endif
  167.     if (write(fd, &dummy, 1) < 0) {
  168.     perror("parent: write");
  169.     exit(1);
  170.     }
  171. #if PARENT_DOES_MALLOC
  172.     CheckBuffer(buf, bufSize, 'b', "parent");
  173. #endif
  174.  
  175.     /* Wait for the child to exit. */
  176.     wait(&status);
  177. }
  178.  
  179. /* Fill the buffer with the given character. */
  180. void
  181. FillBuffer(buf, bufSize, ch)
  182.     char *buf;
  183.     int bufSize;
  184.     char ch;
  185. {
  186.     for (; bufSize > 0; buf++, bufSize--) {
  187.     *buf = ch;
  188.     }
  189. }
  190.  
  191. /* Verify that the buffer is filled with the given character. */
  192. void
  193. CheckBuffer(buf, bufSize, ch, who)
  194.     char *buf;
  195.     int bufSize;
  196.     char ch;
  197.     char *who;            /* "parent" or "child" */
  198. {
  199.     char *bufPtr;
  200.  
  201.     for (bufPtr = buf; bufPtr < buf + bufSize; bufPtr++) {
  202.     if (*bufPtr != ch) {
  203.         fprintf(stderr, "%s: wanted 0x%x, found 0x%x at buf[%d]\n",
  204.             who, ch, *bufPtr, bufPtr - buf);
  205.         exit(1);
  206.     }
  207.     }
  208. }
  209. @
  210.